package com.example.demo.controller;

import com.example.demo.entity.Product;
import com.example.demo.entity.User;
import com.example.demo.entity.VideoSource;
import com.example.demo.service.ProductServiceImpl;
import com.example.demo.service.UserServiceImpl;
import com.example.demo.service.VideoSourceServiceImpl;
import com.example.demo.vo.ProductVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/18 0018.
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    private Logger logger= LoggerFactory.getLogger(ProductController.class);
@Autowired
private ProductServiceImpl productService;
@Autowired
private UserServiceImpl userService;

    @RequestMapping(value = "/test")
    public String test(){
        System.out.print("productController test=----------");
        return "product";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public List<Product> add(ProductVo p){
        System.out.print("productController add=----------"+p.getCode());
        System.out.print("productController add=------setPrice----"+String.valueOf(p.getPrice()));
        System.out.print("productController add=-----getName-----"+p.getName());
        System.out.print("productController add=-----getStanderd-----"+p.getStanderd());
        System.out.print("productController add=-----getSrcPath-----"+p.getSrcPath());
        Product product=new Product();
        product.setName(p.getName());
        product.setPrice(p.getPrice());
        product.setStanderd(p.getStanderd());
        product.setCode(p.getCode());
        product.setSrcPath(p.getSrcPath());
        User user =new User();
        user.setName(p.getUserName());
        product.setUser(user);
        productService.save(product);
        List<Product> productList=productService.findAll();
        return productList;
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public List<Product> del(Long id){
        productService.del(id);
        List<Product> productList=productService.findAll();
        return productList;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public List<Product> update(Product p){
        productService.update(p);
        List<Product> productList=productService.findAll();
        return productList;
    }

    @GetMapping("/getById")
    public Product findById(){
        Product product=productService.findById((long)1);
        System.out.print("product=----------"+product.toString());
        return product;
    }
    @GetMapping("/getAll")
    public List<Product> all(){
        List<Product> productList=productService.findAll();
        System.out.print("-productList==---------"+productList.toString());
        return productList;
    }

    @PostMapping("/findProductByUserName")
    public List<Product> findProductByName(@RequestParam("name")String name){
        User user=userService.findByProperty(name);
        List<Product> productList=productService.findByUserId(user.getId());
        System.out.print("-productList==---------"+productList.toString());
        logger.info("=--=========findProductByName===========");
        return productList;
    }


}
